/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Database;

import Domain.InstructorBooking;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author Min0
 */
public class DBInstructorBooking implements IFDBInstructorBooking
{
    private Connection con;

    public DBInstructorBooking()
    {
        con = DBConnection.getInstance().getDBcon();
    }

    //START private methods for class
    private InstructorBooking BuildActivity(ResultSet results)
    {
        InstructorBooking appObj = new InstructorBooking();
        try
        {
            appObj.setCustomerID(results.getString("CustomerID"));
            appObj.setDate(results.getString("Date"));
            appObj.setIBID(results.getString("IBID"));
            appObj.setInstructorID(results.getString("InstructorID"));
            appObj.setNoOfHours(results.getString("NoOfHours"));
            appObj.setStartTime(results.getString("StartTime"));
            
            
        } catch (Exception e)
        {
            System.out.println("error in building the InstructorBooking object\n" + e.getMessage());
        }
        return appObj;
    }

    private String BuildQuery(String wClause)
    {
        String query = "SELECT * FROM InstructorBooking";

        if (wClause.length() > 0)
        {
            query = query + " WHERE " + wClause;
        }

        return query;
    }

    private InstructorBooking singleWhere(String wClause)
    {
        ResultSet results;
        InstructorBooking appObj = new InstructorBooking();
        String query = BuildQuery(wClause);
        System.out.println(query);
        try
        {
            java.sql.Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            if (results.next())
            {
                appObj = BuildActivity(results);
                //assocaition is to be build
                stmt.close();
            }

        }//end try	
        catch (Exception e)
        {
            System.out.println("Query exception: " + e);
        }
        return appObj;
    }

    private ArrayList<InstructorBooking> miscWhere(String wClause)
    {
        ResultSet results;
        ArrayList<InstructorBooking> list = new ArrayList<InstructorBooking>();

        String query = BuildQuery(wClause);

        try
        { // read the Activity from the database
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            results = stmt.executeQuery(query);

            while (results.next())
            {
                InstructorBooking appObj = new InstructorBooking();
                appObj = BuildActivity(results);
                list.add(appObj);
            }//end while
            stmt.close();
        } catch (Exception e)
        {
            System.out.println("Query exception - select: " + e);
        }
        return list;
    }
    //END private methods for class

    @Override
    public void insertIB(String IBID, String instructorID, String CustomerID, String Date, String startTime, String noOfHours)
    {
        String query = "INSERT into InstructorBooking(IBID, InstructorID, CustomerID, Date, StartTime, NoOfHours) VALUES('"
                + IBID + "','"
                + instructorID + "','"
                + CustomerID + "','"
                + Date + "','"
                + startTime + "','"
                + noOfHours + "')";
        
        System.out.println("insert: " + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        }//end try	
        catch (Exception ex)
        {
            System.out.println("Insert exception in InstructorBooking db: " + ex);
        }
    }

    @Override
    public InstructorBooking getIB(String IBID)
    {
        String wClause = " IBID ='"+IBID+"'";
        return singleWhere(wClause);
    }

    @Override
    public ArrayList<InstructorBooking> getIBList()
    {
        return miscWhere("");
    }

    @Override
    public void updateIB(String IBID, String instructorID, String CustomerID, String Date, String startTime, String noOfHours)
    {
        String query = "UPDATE InstructorBooking SET"
                + "InstructorID = '" + instructorID + "',"
                + "CustomerID = '" + CustomerID + "',"
                + "Date = '" + Date + "',"
                + "StartTime = '" + startTime + "',"
                + "NoOfHours = '" + noOfHours + "'"
                + "WHERE IBID = '" + IBID+"'";
        
        System.out.println("Update query:" + query);
        try
        {
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Update exception in InstructorBooking db: " + ex);
        }
    }

    @Override
    public void deleteIB(String IBID)
    {
        String query = "DELETE from InstructorBooking WHERE IBID = '"+IBID+"'";
        
        System.out.println(query);
        try
        { // delete
            Statement stmt = con.createStatement();
            stmt.setQueryTimeout(5);
            stmt.executeUpdate(query);
            stmt.close();
        } catch (Exception ex)
        {
            System.out.println("Delete exception in InstructorBooking db: " + ex);
        }
    }

    @Override
    public int getMax()
    {
        int max = getMax.getMaxId("Select max(IBID) from InstructorBooking");
        return max;
    }

}
